﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TOOLS;
namespace BLL
{
    public  class CDKey
    {
        public static List<Model.tb_cdkey> GetCDKeys(Dictionary<string, string> p)
        {
            var dalCdkey = new DAL.Cdkey();
            System.Linq.Expressions.Expression<Func<Model.tb_cdkey,bool>> where= a=>true;
            if (p.ContainsKey("script_id"))
            {
                var temp=p["script_id"].ToInt();
                where = where.And(a => a.script_id == temp);
            }
          
            if (p.ContainsKey("create_time_start"))
            {
                DateTime dtTemp=Convert.ToDateTime( p["create_time_start"]);
                where = where.And(a => a.create_time >= dtTemp);
            }
            if (p.ContainsKey("create_time_end"))
            {
                DateTime dtTemp = Convert.ToDateTime(p["create_time_end"]);
                where = where.And(a => a.create_time <= dtTemp);
            } if (p.ContainsKey("use_time_start"))
            {
                DateTime dtTemp = Convert.ToDateTime(p["use_time_start"]);
                where = where.And(a => a.use_time >= dtTemp);
            }
            if (p.ContainsKey("use_time_end"))
            {
                DateTime dtTemp = Convert.ToDateTime(p["use_time_end"]);
                where = where.And(a => a.use_time <= dtTemp);
            }
            if (p.ContainsKey("value"))
            {    var temp=p["value"].ToInt();
                where = where.And(a =>a.value== temp);
            }
            if (p.ContainsKey("user_name"))
            {
                var temp = p["user_name"];
                where = where.And(a => a.use_user == temp);
            }
            if (p.ContainsKey("user_id"))
            {
                var temp=p["user_id"].ToInt();
                where = where.And(a => a.use_user_id ==temp );
            }

            if (p.ContainsKey("is_use"))
            {
                bool temp= bool.Parse(p["is_use"]);
                where = where.And(a => a.is_used ==temp);
            }
            if (p.ContainsKey("cdkey"))
            {
                var temp = p["cdkey"].Split("\r\n");
                where = where.And(a =>temp.Contains(a.cdkey) );
            }
            if (p.ContainsKey("note"))
            {
                 var temp = p["note"];
                where = where.And(a =>a.note==temp);
            }
            return dalCdkey.GetList(where).ToList();
        }

        public static bool CreateCDKey(Dictionary<string, string> p)
        {
            var dalCdkey = new DAL.Cdkey();
            List<tb_cdkey> addList = new List<tb_cdkey>();
            for (int i = 0; i < p["num"].ToInt(); i++)
            {
                tb_cdkey temp = new tb_cdkey();
                temp.cdkey = Guid.NewGuid().ToString().Replace("-", "");
                temp.value = p["value"].ToInt();
                temp.create_time = DateTime.Now;
                temp.is_used = false;
                temp.script_id = p["script_id"].ToInt();
                temp.note = p["note"];
                addList.Add(temp);
            }
           return dalCdkey.Add(addList);
        }
    }
}
